﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using InstituteManager.Bussiness;
using InstituteManager.Data;
using System.Security.Cryptography;
using System.IO;

namespace InstituteManager.adm
{
    public partial class PostingXL : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {   
            pnlMessage.Visible = false;
            if (!IsPostBack)
            {
                lbtDeleteT.Attributes.Add("onClick", "javascript:return confirm(\"Are you sure ?\")");
                lbtDeleteB.Attributes.Add("onClick", "javascript:return confirm(\"Are you sure ?\")");
                gridBind();
            }
        }

        protected void GeneralDateControls(DropDownList ddlDay, DropDownList ddlMon, DropDownList ddlYear)
        {
            for (int i = 1; i <= 31; i++)
                ddlDay.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1; i <= 12; i++)
                ddlMon.Items.Add(new ListItem(i + "", i + ""));
            for (int i = 1900; i <= DateTime.Now.Year; i++)
                ddlYear.Items.Add(new ListItem(i + "", i + ""));
        }

        protected void gridBind()
        {
            grvView.DataSource = PostingBL.getByAll();
            grvView.DataBind();
        }

        protected string getCompetitionName(string PostingId)
        {
            if (PostingBL.getById(int.Parse(PostingId)).tbl_Competition != null)
                return PostingBL.getById(int.Parse(PostingId)).tbl_Competition.CompetitionName + "&nbsp;&nbsp;&nbsp;";
            return "";
        }

        protected bool IsCompetitionExpired(string PostingId)
        {
            if (PostingBL.getById(int.Parse(PostingId)).tbl_Competition != null)
                return CompetitionBL.isExpired(PostingBL.getById(int.Parse(PostingId)).tbl_Competition.CompetitionId);
            return false;
        }

        public string GetMD5(string chuoi)
        {
            string str_md5 = "";
            byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);

            MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
            mang = my_md5.ComputeHash(mang);

            foreach (byte b in mang)
            {
                str_md5 += b.ToString("X2").ToLower();
            }

            return str_md5;
        }
        protected void grvView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
                ((CheckBox)e.Row.FindControl("chkSelectAll")).Attributes.Add("onClick", "SelectAllClick(this)");
            if (e.Row.RowType == DataControlRowType.DataRow)
                ((CheckBox)e.Row.FindControl("chkSelect")).Attributes.Add("onClick", "SelectClick(this)");
        }

        protected void lbtAddnew_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "Sorry, You do not have sufficient privileges";
            lblMessage.Visible = true;
        }

        protected void lbtBack_Click(object sender, EventArgs e)
        {
            pnlView.Visible = true;
        }

        protected void lbtSave_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "Sorry, You do not have sufficient privileges";
            pnlView.Visible = true;
            gridBind();
        }


        protected void ShowMessage(int MegType, string Message)
        {
            pnlMessage.Visible = true;
            ((Label)pnlMessage.FindControl("lblMessage")).Text = Message;
            switch (MegType)
            {
                case 1:
                    pnlMessage.CssClass = "sukses";
                    break;
                case 0:
                    pnlMessage.CssClass = "gagal";
                    break;
                default:
                    pnlMessage.CssClass = "informasi";
                    break;
            }
        }

        protected void grvView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvView.PageIndex = e.NewPageIndex;
            gridBind();
        }

        protected void grvView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            string strId = e.CommandArgument.ToString();
            Data.tbl_Posting obj = new tbl_Posting();
            switch (e.CommandName)
            {
                case "cmdView":
                    obj = PostingBL.getById(int.Parse(strId));
                    lblPostingName.Text = obj.PostingName;
                    if (obj.PostDate != null)
                        lblPostDate.Text = obj.PostDate.Value.ToString("d");
                    lblDescription.Text = obj.Description;
                    lblPrice.Text = obj.Price.ToString();
                    if (obj.tbl_Competition != null)
                        lblCompetitionName.Text = obj.tbl_Competition.CompetitionName;
                    lblMark.Text = obj.Mark.ToString();
                    lblRemark.Text = obj.Remark;
                    imgImage.ImageUrl = obj.ImageUrl;

                    pnlView.Visible = false;
                    pnlDetail.Visible = true;
                    break;                
            }
        }

        protected void lbtRefresh_Click(object sender, EventArgs e)
        {
            gridBind();
        }

        protected void lbtDelete_Click(object sender, EventArgs e)
        {
            lblMessage.Text = "Sorry, You do not have sufficient privileges";
            lblMessage.Visible = true;
        }

        protected void ibtSearch_Click(object sender, ImageClickEventArgs e)
        {
            grvView.DataSource = PostingBL.SearchByName(txtSearch.Text);
            DataBind();
        }

        protected void lbtBackDetail_Click(object sender, EventArgs e)
        {
            pnlDetail.Visible = false;
            pnlView.Visible = true;
        }
    }
}